package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.EvenGroupModel;

public class EventGroup {
	
	public EventGroup() {}
	
	
	public List<EvenGroupModel> ListEGroups() {
		List<EvenGroupModel> list = new ArrayList<EvenGroupModel>();
		Connection con = new Connect().getConnection();
		String sql = "SELECT * FROM EVENTGROUP ORDER BY ID DESC";
		try {
			PreparedStatement state = con.prepareStatement(sql);
			ResultSet rs = state.executeQuery();
			while (rs.next()) {
				list.add(new EvenGroupModel(
						rs.getString("id"),
						rs.getString("name")));
			}
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	public boolean UpdateEventGroup(EvenGroupModel eg) {
		Connection con = new Connect().getConnection();
		String sql = "UPDATE EVENTGROUP SET ID = '" + eg.getId()+ "', NAME = '"+ eg.getName()+ "'";
		try {
			PreparedStatement state = con.prepareStatement(sql);
			state.executeUpdate();
			con.close();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public void DeleteEventGroup(String id) {
		Connection con = new Connect().getConnection();
		String sql = "DELETE FROM EVENTGROUP WHERE ID = '" + id + "'";
		try {
			PreparedStatement state = con.prepareStatement(sql);
			state.executeUpdate();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public boolean CreateEventGroup(EvenGroupModel eg) {
		Connection con = new Connect().getConnection();
		String sql = "INSERT INTO EVENTGROUP VALUES('" + eg.getId()+ "', '" + eg.getName()+"')";
		try {
			PreparedStatement state = con.prepareStatement(sql);
			state.executeUpdate();
			con.close();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean CheckExistEventGroup(String id) {
		Connection con = new Connect().getConnection();
		String sql = "SELECT ID FROM EVENTGROUP WHERE ID = '" +id 
				+ "'";
		try {
			PreparedStatement state = con.prepareStatement(sql);
			ResultSet rs = state.executeQuery();
			if (rs.next())
				return true;
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
}

